package org.swim.servlet;

import java.util.List;
import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.swim.entity.Ability;
import org.swim.entity.HelpRequests;
import org.swim.entity.User;
import org.swim.session.FriendshipManagerRemote;
import org.swim.session.HelpRequestManagerRemote;
import org.swim.session.UserManagerRemote;

/**
 * Servlet implementation class Profile
 */
public class Profile extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Profile() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		User me = (User) session.getAttribute("user");
        int profileToView = Integer.parseInt(request.getParameter("id"));
		try {
        	String userId = (String) request.getParameter("id");
        	if (userId.equals("null") || (Integer.parseInt(userId) == me.getId())){
             	profileToView = ((User) session.getAttribute("user")).getId();
            	request.setAttribute("isMyProfile", true);   
        	} else {
             	profileToView = Integer.parseInt(userId);
             	request.setAttribute("isMyProfile", false);		
        	}
        }
        catch (NullPointerException e ) {
			request.setAttribute("isMyFriend", false);
			request.setAttribute("pendingFriendship", false);
         	request.setAttribute("isMyProfile", false);		
        }
		InitialContext ic;
        try {
			ic = new InitialContext();
			UserManagerRemote userManager = (UserManagerRemote)ic.lookup("UserManagerRemote");
			HelpRequestManagerRemote helpManager = (HelpRequestManagerRemote) ic.lookup("HelpRequestManagerRemote");
			User user = userManager.getUser(profileToView);
			request.setAttribute("userProfile", user);
			request.setAttribute("profileToView", user.getId());
			String status = (String) session.getAttribute("status");
			List<HelpRequests> requests = helpManager.getRecievedCompletedRequest(user.getId());
			request.setAttribute("requests", requests);
			FriendshipManagerRemote friendshipManager = (FriendshipManagerRemote)ic.lookup("FriendshipManagerRemote");
			if(!status.equals("guest")) {
				if (friendshipManager.areFriend(me.getId(), profileToView)) {
					request.setAttribute("isMyFriend", true);
				} else {
					request.setAttribute("isMyFriend", false);
				}
				if (friendshipManager.pendingFriendRequest(me.getId(), profileToView)) {
					request.setAttribute("pendingFriendship", true);
				} else {
					request.setAttribute("pendingFriendship", false);
				}						
			} else {
				request.setAttribute("isMyFriend", false);				
				request.setAttribute("pendingFriendship", false);
	         	request.setAttribute("isMyProfile", false);		
	         }
			List<User> friends = friendshipManager.getAllUserFriend(user.getId());
			request.setAttribute("friends", friends);					
			List<Ability> abilities = userManager.getAbilityListByID(user.getId());
			request.setAttribute("abilities", abilities);		
        } catch (NamingException e) {
			e.printStackTrace();
		} 
		request.getRequestDispatcher("WEB-INF/Profile.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
